package com.lhd.controller;


import com.alibaba.fastjson.JSONObject;
import com.lhd.bean.GoodsUvCt;
import com.lhd.service.GoodsService;
import com.lhd.service.GoodsService1;
import com.lhd.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/gmall/realtime/user1")
public class GoodsController1 {

    @Autowired
    private GoodsService1 goodsService;

    @RequestMapping("/goodsUv1")
    public String getUvPerPage(
            @RequestParam(value = "date", defaultValue = "1") Integer date) {
        if (date == 1) {
            date = DateUtil.now();
        }
        List<JSONObject> uvList = goodsService.getGoodsUv(date);
        if (uvList == null || uvList.isEmpty()) {
            return "";
        }
        StringBuilder rows = new StringBuilder("[");
        for (int i = 0; i < uvList.size(); i++) {
            JSONObject goodsUvCt = uvList.get(i);
            Integer uvCt = goodsUvCt.getInteger("uvCt");
            rows.append("{\n" +
                    "        \"uv_ct\": \"" + (uvCt != null ? uvCt : 0) + "\"\n" +
                    "      }");
            if (i < uvList.size() - 1) {
                rows.append(",");
            } else {
                rows.append("]");
            }
        }
        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"columns\": [\n" +
                "      {\n" +
                "        \"name\": \"商品访客数\",\n" +
                "        \"id\": \"uv_ct\"\n" +
                "      }\n" +
                "    ],\n" +
                "    \"rows\": " + rows + "\n" +
                "  }\n" +
                "}";
    }



}
